import time
import multiprocessing
from wish.app.get_role_up_times import once_need_times

title = '抽1只up'
process_count = 32
get_times_per_process = 200000
wish_times = 200
before_pro_key_point = [0.1, 0.25, 0.5, 0.75, 0.9]


def process_func(result_list, process_num, process_lock):
    rl_process = [0 for _ in range(wish_times)]
    process_name = multiprocessing.current_process().name
    gt = get_times_per_process
    percent = gt / 100
    for index in range(gt):
        ot = once_need_times()
        rl_process[ot - 1] += 1
        if index % percent == 0:
            print(f'{process_name} - {index / percent}/100')
    process_lock.acquire()
    result_list.append(rl_process)
    process_lock.release()
    print('process', process_num, 'complete.')


if __name__ == '__main__':
    t = time.time()
    process_list = []
    lock = multiprocessing.Lock()
    rll = multiprocessing.Manager().list()
    rv = [0 for _ in range(wish_times)]
    for i in range(process_count):
        process_list.append(multiprocessing.Process(
            target=process_func, args=(rll, i, lock)))
    for process in process_list:
        process.start()
    for process in process_list:
        process.join()

    total_times = 0
    for li in rll:
        for i in range(len(li)):
            rv[i] += li[i]
            total_times += li[i] * i

    total_get = 0
    total_get_times = process_count * get_times_per_process
    key_point_map = {}
    print('第n抽', '本抽获取概率', '本抽内获取概率')
    for i in range(len(rv)):
        v = rv[i]
        total_get += v
        before_pro = total_get / total_get_times
        for k in before_pro_key_point:
            if before_pro > k and k not in key_point_map:
                key_point_map[k] = i
                break
        print('%d %.5f %.5f' %
              (i + 1, v / total_get_times, before_pro))

    print()
    print(title)
    for k in before_pro_key_point:
        print('有', '%.02f' % k, '概率在', key_point_map[k], '抽内抽到')
    print('平均', '%.2f' % (total_times / total_get_times), '次出')
    print()
    print('run in:', time.time() - t, 's')
